package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Attendance;
import com.example.result.Result;
import com.example.service.AttendanceService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 考勤控制层
 * @date 2019-12-31
 */
@RestController
@RequestMapping("/api")
public class AttendanceController {

    @Autowired
    AttendanceService attendanceService;

    //==============================考勤管理================================================

    /**
     * @param json
     * @return 添加成功则返回成功信息，反之则提示失败
     * @describe 添加考勤信息
     */
    @RequestMapping(value = "/attendances", method = RequestMethod.POST)
    public Result addAttendance(@RequestBody String json) {
        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Attendance attendance = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Attendance.class);
        return attendanceService.addAttendance(attendance);

    }

    /**
     * @param json
     * @return 更新成功则返回成功信息，反之则提示失败
     * @describe 更新考勤信息
     */
    @RequestMapping(value = "/attendances", method = RequestMethod.PUT)
    public Result updateAttendance(@RequestBody String json) {
        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Attendance attendance = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Attendance.class);
        return attendanceService.updateAttendance(attendance);

    }

    /**
     * @param attendanceId
     * @return 删除成功则返回成功信息，反之则提示失败
     * @describe 根据编号删除考勤信息
     */
    @RequestMapping(value = "/attendances/{attendanceId}", method = RequestMethod.DELETE)
    public Result deleteAttendanceById(@PathVariable("attendanceId") String attendanceId) {
        return attendanceService.deleteAttendanceById(attendanceId);

    }

    /**
     * @return 以JSON格式返回所有考勤信息
     * @describe 查询所有考勤信息
     */
    @RequestMapping(value = "/attendances", method = RequestMethod.GET)
    public Result queryAllAttendance() {
        return attendanceService.queryAllAttendance();

    }

    /**
     * @param attendanceId
     * @return 以JSON格式返回该考勤信息
     * @describe 根据编号查询考勤信息
     */
    @RequestMapping(value = "/attendances/{attendanceId}", method = RequestMethod.GET)
    public Result queryAttendanceById(@PathVariable("attendanceId") String attendanceId) {
        return attendanceService.queryAttendanceById(attendanceId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加考勤信息
     */
    @RequestMapping(value = "/attendances/excels", method = RequestMethod.POST)
    public Result addAttendanceByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Attendance> list = JSON.parseArray(parseObject.getString("json"), Attendance.class);

        System.out.println(list);

        return attendanceService.addAttendanceByExcel(list);

    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 考勤信息模糊查询
     */
    @RequestMapping(value = "/attendances/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return attendanceService.serachByInfo(info);

    }

    /**
     * @param role_Id
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据考勤编号批量删除考勤信息
     */
    @RequestMapping(value = "/attendances/dels", method = RequestMethod.POST)
    public Result deleteAttendanceServiceByIds(@RequestBody Map<String, String> attendanceIds) {

        return attendanceService.deleteAttendanceServiceByIds(attendanceIds);
    }


    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生的考勤信息
     */
    @RequestMapping(value = "/attendances/studentAndAttendances", method = RequestMethod.GET)
    public Result queryAllStudentAndAttendanceInfo() {
        return attendanceService.queryAllStudentAndAttendanceInfo();
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生ID查询学生考勤信息
     */
    @RequestMapping(value = "/attendances/attendanceByUserId", method = RequestMethod.POST)
    public Result selectAttendanceByUserId(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        Integer userId = Integer.parseInt(parseObject.getString("json"));

        return attendanceService.selectAttendanceByUserId(userId);
    }
}
